package com.handong.handserialprogram.module.devicetask;

import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.handong.handserialprogram.R;
import com.handong.handserialprogram.constants.ColorContant;
import com.handong.handserialprogram.constants.KeyConstant;
import com.handong.handserialprogram.model.entities.FootRingNewEntity;
import com.handong.handserialprogram.model.status.FootRingStatus;
import com.handong.handserialprogram.module.singleperson.SinglePersonActivity;

import java.util.List;

/**
 * Created by userKey on 2017/8/3.
 */

public class DeviceListAdapter extends RecyclerView.Adapter<DeviceListAdapter.DeviceHolder> {

    private List<FootRingNewEntity> mList;
    private DeviceTaskActivity mActivity;

    public DeviceListAdapter(List<FootRingNewEntity> list, DeviceTaskActivity activity) {
        mList = list;
        mActivity = activity;
    }

    @Override
    public DeviceHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_device_item, parent, false);
        return new DeviceHolder(rootView);
    }

    @Override
    public void onBindViewHolder(DeviceHolder holder, int position) {
        FootRingNewEntity entity = mList.get(position);
        holder.mEntity = entity;
        GradientDrawable drawable = (GradientDrawable) holder.status.getBackground();
        if (entity.getStatus() == FootRingStatus.ESCAPE) {
            holder.status.setText(R.string.ring_status_escape);
            drawable.setColor(ColorContant.PERSON_ESCAPE);
        } else {
            if (entity.getStatus() == FootRingStatus.NORMAL && entity.getActualRingDistance() < entity.getSetRingDistance()) {
                holder.status.setText(R.string.ring_status_normal);
                drawable.setColor(ColorContant.PERSON_NORMAL);
            } else {
                if (entity.getStatus()==FootRingStatus.NORMAL)
                {
                    holder.status.setText(R.string.ring_status_exception);
                }
                else{
                    holder.status.setText(R.string.ring_status_week);
                }
                drawable.setColor(ColorContant.PERSON_EXCEPTION);
            }
        }
        holder.id.setText(entity.getRingID());
        holder.distance.setText(entity.getActualRingDistance() + "m");
        holder.setDistance.setText(entity.getSetRingDistance() + "m");
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public class DeviceHolder extends RecyclerView.ViewHolder {

        private FootRingNewEntity mEntity;
        private TextView id, distance, setDistance, status;

        public DeviceHolder(View itemView) {
            super(itemView);
            id = (TextView) itemView.findViewById(R.id.deviceID);
            distance = (TextView) itemView.findViewById(R.id.deviceActualDistance);
            setDistance = (TextView) itemView.findViewById(R.id.deviceSetDistance);
            status = (TextView) itemView.findViewById(R.id.deviceStatus);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle2 = new Bundle();
                    bundle2.putString(KeyConstant.PERSON_NAME, "脚环 - " + mEntity.getRingID());
                    bundle2.putFloat(KeyConstant.PERSON_DISTANCE, mEntity.getActualRingDistance());
                    bundle2.putInt(KeyConstant.PERSON_SET_DISTANCE, mEntity.getSetRingDistance());
                    bundle2.putString(KeyConstant.PERSON_RING_ID, mEntity.getRingID());
                    mActivity.openActivity(SinglePersonActivity.class, bundle2);
                }
            });
        }
    }
}
